package com.m7788.screen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.m7788.dao.HUrlDaoImpl;
import com.m7788.po.HistoryUrl;
import com.m7788.util.AppUtil;
import com.m7788.util.Constant;
import com.m7788.view.UmengBaseActivity;
import com.tool.utils.LogoutUtil;

public class HistoryUrlScreen extends UmengBaseActivity implements OnClickListener,OnItemClickListener{
	ListView listView;
	Button btn_left;
	List<HistoryUrl> dataList;
	private int po;
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.history_url_screen);
        LogoutUtil.getInstance().addActivity(this);
        initView();
    }
    @Override
    public void onResume() {
        super.onResume();
    }
    
    @Override
    public void onDestroy() {
        
        super.onDestroy();
    }
    private void initView(){
    	btn_left =(Button)findViewById(R.id.hus_btn_left);
    	btn_left.setOnClickListener(this);
    	
    	listView =  (ListView)this.findViewById(R.id.hus_listview) ;
		listView.setOnItemClickListener(this);
		listView.setCacheColorHint(0);
		listView.setDivider(getResources().getDrawable(R.drawable.listview_line));
		refreshData();
    }
    
    
    private  synchronized   void refreshData(){
    	HUrlDaoImpl daoImpl = new HUrlDaoImpl(this);
		List<HistoryUrl> dataList = daoImpl.rawQuery(
				"select * from HistoryUrl order by id desc ", new String[] {
						 });
		if(dataList.size()>10){
			for(int i=0;i<10;i++){
				HistoryUrl url=dataList.get(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("url", url.getUrl());
				map.put("title", url.getTitle());
				map.put("po", i+1+"、");
				list.add(map);
			}
		}else{
			for(int i=0;i<dataList.size();i++){
				HistoryUrl url=dataList.get(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("url", url.getUrl());
				map.put("title", url.getTitle());
				map.put("po", i+1+"、");
				list.add(map);
			}
		}
		SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.adapter_listview_category,new String[]{"title","po"},new int[]{R.id.alc_tv_attr1,R.id.alc_tv_attr0});
		listView.setAdapter(adapter);
    }
    
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.hus_btn_left:
			finish();
    		break;
    	default:
    		break;
		}
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Map<String, Object> a = list.get(arg2);
		String url = a.get("url").toString();
		AppUtil.saveRecord(HistoryUrlScreen.this, Constant.INDEX_HISTORY_URL, url);
		finish();
	}
	
	
}